package com.eco.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eco.annotation.AuthToken;
import com.eco.entity.FishType;
import com.eco.entity.RecordDTO;
import com.eco.service.RecordService;
import com.eco.utils.DateAndStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@RestController
public class IRFishController {

    @Resource
    private RecordService recordService;

    @GetMapping({"/getIRFishTable"})
    @AuthToken
    public JSONObject getIRFishTable(String startDate, String endDate, String pageNum, String point) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate) || StringUtils.isEmpty(point)) {
            log.error("有日期或点位参数为空");
            result.put("msg", "有日期或点位参数为空");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }
        List<RecordDTO> recordDTOS = this.recordService.findByRecordDate(startDate, endDate, point, Integer.parseInt(pageNum), 10);
        int total = this.recordService.countByRecordDate(startDate, endDate, point);
        if (CollectionUtils.isEmpty(recordDTOS)) {
            log.error("未查询到红外过鱼数据");
            result.put("msg", "未查询到红外过鱼数据");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        JSONArray datas = new JSONArray();
        recordDTOS.stream().forEach(recordDTO -> {
            JSONObject data = new JSONObject();
            data.put("recordId", recordDTO.getRecordId());
            data.put("recordTime", recordDTO.getRecordTime());
            data.put("width", recordDTO.getWidth());
            data.put("length", recordDTO.getLength());
            data.put("direction", recordDTO.getDirectionStr());
            data.put("fishSpeed", recordDTO.getFishSpeed());
            data.put("videoUrl", recordDTO.getVideoUrl());
            data.put("category", recordDTO.getCategory());
            datas.add(data);
        });
        log.info("查询红外过鱼数据(table)成功");
        result.put("status", Boolean.valueOf(true));
        result.put("data", datas);
        result.put("total", Integer.valueOf(total));
        return result;
    }

    @GetMapping({"/getIRFishChart"})
    @AuthToken
    public JSONObject getIRFishChart(String startDate, String endDate, String point, String mode) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate) || StringUtils.isEmpty(point)) {
            log.error("有日期或点位参数为空");
            result.put("msg", "有日期或点位参数为空");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        if (StringUtils.isEmpty(point)) {
            point = "out";
        }
        if (StringUtils.isEmpty(mode)) {
            mode = "infrared";
        }
        JSONArray datas = new JSONArray();
        String dateStr = DateAndStringUtils.dateNow();
        int up = 0;
        int down = 0;
        int upTotal = 0;
        int downTotal = 0;
        List<RecordDTO> todayRecord = this.recordService.findByRecordDateTime(dateStr + " 00:00:00", dateStr + " 23:59:59", point);
        AtomicInteger upNum = new AtomicInteger();
        AtomicInteger downNum = new AtomicInteger();
        if (CollectionUtils.isEmpty(todayRecord)) {
            upNum.set(0);
            downNum.set(0);
        } else {
            todayRecord.stream().forEach(recordDTO -> {
                if (recordDTO.getDirection().intValue() == 0) {
                    upNum.addAndGet(1);
                } else {
                    downNum.addAndGet(1);
                }
            });
        }
        up = upNum.intValue();
        down = downNum.intValue();
        upTotal = this.recordService.getFishTotalByDirection(point, Integer.valueOf(0));
        downTotal = this.recordService.getFishTotalByDirection(point, Integer.valueOf(1));
        List<RecordDTO> recordDTOS = this.recordService.findByRecordDateTime(startDate, endDate, point);
        if (CollectionUtils.isEmpty(recordDTOS)) {
            log.error("未查询到红外过鱼数据");
            result.put("msg", "未查询到红外过鱼数据");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            result.put("todayUp", Integer.valueOf(up));
            result.put("todayDown", Integer.valueOf(down));
            result.put("todayTotal", Integer.valueOf(up + down));
            result.put("totalUp", Integer.valueOf(upTotal));
            result.put("totalDown", Integer.valueOf(downTotal));
            result.put("totalTotal", Integer.valueOf(upTotal + downTotal));
            return result;
        }
        recordDTOS.stream().forEach(recordDTO -> {
            JSONObject data = new JSONObject(new LinkedHashMap<>());
            data.put("recordTime", recordDTO.getRecordTime());
            data.put("length", recordDTO.getLength());
            data.put("width", recordDTO.getWidth());
            data.put("fishSpeed", recordDTO.getFishSpeed());
            data.put("direction", recordDTO.getDirection());
            data.put("category", recordDTO.getCategory());
            datas.add(data);
        });
        log.info("查询红外过鱼数据成功");
        result.put("status", Boolean.valueOf(true));
        result.put("data", datas);
        result.put("todayUp", Integer.valueOf(up));
        result.put("todayDown", Integer.valueOf(down));
        result.put("todayTotal", Integer.valueOf(up + down));
        result.put("totalUp", Integer.valueOf(upTotal));
        result.put("totalDown", Integer.valueOf(downTotal));
        result.put("totalTotal", Integer.valueOf(upTotal + downTotal));
        return result;
    }

    @PostMapping(value = {"/updateIRFish"}, consumes = {"application/json"})
    @AuthToken("admin")
    public JSONObject updateIRFish(@RequestBody JSONObject data) {
        JSONObject result = new JSONObject();
        String recordId = data.getString("recordId").trim();
        if (StringUtils.isEmpty(recordId)) {
            log.error("recordId或category为空");
            result.put("msg", "recordId或category为空");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        RecordDTO recordDTO = data.toJavaObject(RecordDTO.class);
        if (recordDTO == null) {
            log.error("JSONObject转RecordDTO异常");
            result.put("msg", "数据转换异常");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        int isok = this.recordService.updateByRecord(recordDTO);
        if (isok < 1) {
            log.error("修改失败，操作数据库异常");
            result.put("msg", "操作数据库异常");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        log.info("修改成功");
        result.put("msg", "修改成功");
        result.put("status", Boolean.valueOf(true));
        result.put("data", null);
        return result;
    }

    @RequestMapping({"/deleteIRFish"})
    @AuthToken("admin")
    public JSONObject deleteIRFish(int[] ids) {
        JSONObject result = new JSONObject();
        if (ids.length == 0) {
            log.error("id数组为空");
            result.put("msg", "id数组为空");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        this.recordService.deleteByRecordIds(ids);
        log.info("删除成功");
        result.put("msg", "删除成功");
        result.put("status", Boolean.valueOf(true));
        result.put("data", null);
        return result;
    }

    @GetMapping({"/getIRFishName"})
    @AuthToken
    public JSONObject getIRFishName(String point) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(point)) {
            log.error("区分点位的参数为空");
            result.put("msg", "dataType为空");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        List<FishType> allFishType = this.recordService.getFishName(point);
        if (CollectionUtils.isEmpty(allFishType)) {
            log.error("没有种类数据");
            result.put("status", Boolean.valueOf(false));
            result.put("data", null);
            return result;
        }
        JSONArray datas = new JSONArray();
        allFishType.stream().forEach(aiFishTypeEntity -> {
            JSONObject data = new JSONObject();
            data.put("fishName", aiFishTypeEntity.getFishName());
            datas.add(data);
        });
        log.info("查询种类成功");
        result.put("status", Boolean.valueOf(true));
        result.put("data", datas);
        return result;
    }
}
